package db

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"

	"project/study-go/config"
	"project/study-go/logger"
)

//connMysql 连接mysql数据库
func connMysql() *gorm.DB {
	var (
		conf gorm.Config
		dsn  string
	)
	conf = gorm.Config{
		Logger: logger.DBLogger,
	}
	dsn = config.GenarateMysqlDsn(config.Vip)
	master, _ := gorm.Open(mysql.Open(dsn), &conf)
	db, _ := master.DB()
	db.SetMaxOpenConns(config.Vip.GetInt("mysql.MaxOpenConns"))
	db.SetMaxIdleConns(config.Vip.GetInt("mysql.MaxIdleConns"))
	return master
}

func closeMysql(master *gorm.DB) error {
	db, _ := master.DB()
	return db.Close()
}

//检查 Mysql 是否健康
func checkMysql(master *gorm.DB) error {
	db, _ := master.DB()
	return db.Ping()
}
